import Toast from 'vant-weapp/toast/toast';
const db = wx.cloud.database()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      name: "",
      idcard: "",
      age: "",
      phone: "",
      gender: "男",
      img:""
    },
    gender: [{
      text: "女",
      value: "0"
    }, {
      text: "男",
      value: "1"
    }],
    columns: ['养老院1', '养老院2', '养老院3', '养老院4', '养老院5'],
    show: false,
    fileList: []
  },
  onGender(e) {
    this.data.info.gender = e.detail
    console.log(this.data.info)
  },
  onChange(e) {
    const param = e.currentTarget.dataset.name; //字段名 
    const value = e.detail; //值 
    const key = `info.${param}`;
    this.setData({
      [key]: value
    })
  },
  toShow() {
    this.setData({
      show: true
    })
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  submit() {
    if (this.data.info.name == "") {
      wx.showToast({
        icon: 'error',
        title: '请输入姓名'
      })
    } else if (this.data.info.idcard=="" || this.data.info.idcard.length != 18) {

      wx.showToast({
        icon: 'error',
        title: '请输入身份证号'
      })
    } else if (this.data.info.age=="") {
      wx.showToast({
        icon: 'error',
        title: '请输入年龄'
      })
    } else if (this.data.info.phone==""||this.data.info.phone.length != 11) {
      console.log(this.data.info.phone.length)
      wx.showToast({
        icon: 'error',
        title: '请输入联系方式'
      })
    } else if(this.data.info.img==""){
      wx.showToast({
        icon: 'error',
        title: '请上传照片'
      })
    }
    
    else {
      //查找身份证是否已上传
      db.collection('oldmanlist').where({
        id_card: this.data.info.idcard,
      }).get({
        success: num => {
          var num = num.data.length;
          console.log(num)
          if (num > 0) {
            //存在就更改
            console.log("存在")
            db.collection('oldmanlist').where({
              idCard: oldIdCard,
            }).update({
              data: {
                age:this.data.info.age,
                name: this.data.info.name,
                sex: this.data.info.gender,
                phone: this.data.info.phone,
                id_card: this.data.info.idcard,
                avatar: this.data.info.img
              }
            })
            wx.showToast({
              icon: 'success',
              title: '修改成功',
              duration: 1000
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 1000);
          
          } else {
            //不存在就添加
            console.log("不存在")
            db.collection('oldmanlist').add({
              // data 字段表示需新增的 JSON 数据
              data: {
                age:this.data.info.age,
                name: this.data.info.name,
                sex: this.data.info.gender,
                phone: this.data.info.phone,
                id_card: this.data.info.idcard,
                avatar: this.data.info.img
              }
            })
            wx.showToast({
              icon: 'success',
              title: '绑定成功',
              duration: 1000
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 1000);

          }
        }
      })
    }
  },
  onConfirm(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    console.log(value)
    this.setData({
      show: false,
      "info.yly": value
    })
  },
  // 从相册选取
  afterRead(event) {
    const {
      file
    } = event.detail;
    console.log(file.tempFilePath)
    let suffix = /\.[^\.]+$/.exec(file.tempFilePath)[0];
    console.log(suffix)
    wx.cloud.uploadFile({
      // 指定上传到的云路径
      cloudPath: 'oldImg/' + new Date().getTime() + suffix,
      // 指定要上传的文件的小程序临时文件路径
      filePath: file.tempFilePath,
      // 成功回调
      success: res => {
      
        console.log('上传成功', res.fileID)
        this.setData({
          'info.img': res.fileID
        })
        this.data.fileList.push({
          url: res.fileID
        })
        this.setData({
          fileList: this.data.fileList
        })
        console.log(this.data.info.img)
      }
    })
  },

})